/**
 * @description 路由拦截状态管理，目前两种模式：all模式与intelligence模式，其中partialRoutes是菜单暂未使用
 */
import {asyncRoutes, constantRoutes} from '@/router'

const state = () => ({
    routes: [],
    cachedRoutes: [],
})
const getters = {
    routes: (routeState) => routeState.routes,
    cachedRoutes: (routeState) => routeState.cachedRoutes,
}
const mutations = {
    /**
     * @description 多模式设置路由
     * @param {*} routeState
     * @param {*} routes
     */
    setRoutes(routeState, routes) {
        routeState.routes = routes;
    },
    /**
     * @description 设置缓存Name数组
     * @param {*} routeState
     * @param {*} routes
     */
    setCachedRoutes(routeState, routes) {
        routeState.cachedRoutes = routes;
    },
    /**
     * @description 修改Meta
     * @param {*} routeState
     * @param options
     */
    changeMenuMeta(routeState, options) {
        function handleRoutes(routes) {
            return routes.map((route) => {
                if (route.name === options.name) {
                    Object.assign(route.meta, options.meta)
                }
                if (route.children && route.children.length) {
                    route.children = handleRoutes(route.children)
                }
                return route;
            })
        }

        routeState.routes = handleRoutes(routeState.routes);
    },
}
const actions = {
    /**
     * @description 多模式设置路由
     * @param {*} { commit }
     * @param mode
     * @returns
     */
    async setRoutes({commit}, mode = 'none') {
        // 默认前端路由
        let routes = [...constantRoutes, ...asyncRoutes];
        // 设置菜单所需路由
        commit('setRoutes', routes);
    },
    /**
     * @description 设置缓存Name数组
     * @param {*} { commit }
     * @param {*} routes
     */
    setCachedRoutes({commit}, routes) {
        commit('setCachedRoutes', routes);
    },
    /**
     * @description 修改Route Meta
     * @param {*} { commit }
     * @param options
     */
    changeMenuMeta({commit}, options = {}) {
        commit('changeMenuMeta', options)
    },
}
export default {state, getters, mutations, actions}
